<?php
/**
 * system_constants.php
 *
 * @category   icestorm
 * @package    ICESTORM
 * @author     Magnus Jonsson <magnus.jonsson@codealliance.se>
 * @copyright  2008 Team babel
 * @license    LGPL
 */

/**
 * Storage Locations
 */
define('_ICE_STORAGE_LOCATION_ID_LOCAL_DISK',0);
define('_ICE_STORAGE_LOCATION_ID_AMAZON_S3',3000);

/**
 * Log Settings
 */
define('_ICE_LOG_DB_ENABLE',true);
define('_ICE_LOG_MAX_ENTRIES',30);

/**
 * Garbage Collector Probability for Upload Request in Percentage
 */
define('_ICE_UPLOAD_REQUEST_GC_PROBABILITY',5);

/**
 * Upload Requests Lifetime in Seconds.
 */
define('_ICE_UPLOAD_REQUEST_LIFETIME',3600);

/**
 * Garbage Collector Probability for Upload Request in Percentage
 */
define('_ICE_DOWNLOAD_REQUEST_GC_PROBABILITY',5);

/**
 * Upload Requests Lifetime in Seconds.
 */
define('_ICE_DOWNLOAD_REQUEST_LIFETIME',3600);

/**
 * Category group defines
 */
//CT = "Category Type" defines, binary mask
define( '_CT_PRIVATE',		0x00 );
define( '_CT_FAMILY',		0x01 );
define( '_CT_FRIENDS',		0x02 );
define( '_CT_BUSINESS',		0x04 );
define( '_CT_OTHER',		0x08 );
define( '_CT_PROTECTED',	0x40 ); //Reserved for potential future introduction (e.g. custom groups)
define( '_CT_PUBLIC', 		0x80 );
define( '_CT_ALLOWED', 		0x8F );

/**
 * Profile Picture to be used when no personal picture exists
 */
define('_ICE_INCOGNITO_IMAGE_URL_SMALL','no_photo-s.gif');
define('_ICE_INCOGNITO_IMAGE_URL_MEDIUM','no_photo-m.gif');
define('_ICE_INCOGNITO_IMAGE_URL_LARGE','no_photo-l.gif');
define('_ICE_PROFILE_PICTURE_FILE_TYPE','jpg');
define('_ICE_PROFILE_PICTURES_BUCKET','users.expono.com');

/**
 * Profile Picture sizes
 */
define('_ICE_PROFILE_PICTURE_SIZE_SMALL',	'1');
define('_ICE_PROFILE_PICTURE_SIZE_MEDIUM',	'2');
define('_ICE_PROFILE_PICTURE_SIZE_LARGE',	'3');

/**
 * Current Page URL as used by createPageHash() and destoryPageHash()
 */
define('_CURRENT_PAGE_URL',parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH));

/**
 * Convert ISO Date to Timestamp
 *
 * @param string ISO Date
 * @return integer Timestamp
 */
function timestamp($iso_date) {
	return strtotime($iso_date);
}

/**
 * Round number up
 *
 * Example: round(1.3,0) = 2.0
 *
 * @param decimal $value
 * @param integer $dp Decimal Points
 * @return integer
 */
function roundup ($value, $dp)
{
    return ceil($value*pow(10, $dp))/pow(10, $dp);
}

/**
 * Create Page Hash for current REQUEST URL
 *
 * Use page hash in forms for added security
 *
 * @param string $url_path (optional)
 * @param boolean $regenerate (optional)
 * @return string Hash
 */
function createPageHash($url_path=false,$regenerate=false) {
	if (!$url_path) {
		$url_path = _CURRENT_PAGE_URL;
	}
	if ($url_path=='/') $url_path = '/index.php';
	if (!is_array($_SESSION['page_hash'])) {
		$_SESSION['page_hash']=array();
	}
	if (!$_SESSION['page_hash'][$url_path] || $regenerate) {
		$_SESSION['page_hash'][$url_path] = md5(uniqid());
	}
	return $_SESSION['page_hash'][$url_path];
}

/**
 * Destory Page Hash for current REQUEST_URL
 *
 * @param string $url_path (optional)
 */
function destroyPageHash($url_path=false) {
	if (!$url_path) {
		$url_path = _CURRENT_PAGE_URL;
	}
	unset($_SESSION['page_hash'][$url_path]);
}

/**
 * Check if a Page Hash is valid
 *
 * @param string $hash
 * @param string $url_path (optional)
 * @param boolean $auto_destroy (optional)
 * @return boolean TRUE if valid, FALSE if not valid
 */
function isPageHashValid($hash,$url_path=false,$auto_destroy=true) {
	$key = array_search($hash,$_SESSION['page_hash']);
	if (!$key) return false;
	if ($url_path) {
		if ($key <> $url_path) return false;
	}
	if ($auto_destroy) destroyPageHash($key);
	return true;
}

/**
 * Classic array indexOf function.
 * Returns the first position for specified value in the array or false if the array doesn't
 * contain the value.
 *
 * @param mixed $needle
 * @param array $haystack
 * @return integer - position of the first occurence or false.
 */
function indexOf($needle, $haystack) {
	return array_search($needle, $haystack);
}

/**
 * Check if the code is executing on the LIVE PRODUCTION Server or not.
 *
 * WARNING: NEVER SET _ICE_LIVER_SERVER = TRUE ON YOUR LOCAL DEVELOPER MACHINE!!!
 *
 * @return boolean TRUE if live, FALSE if not
 */
function isLive() {
	if (defined("_ICE_LIVE_SERVER") && _ICE_LIVE_SERVER == TRUE) {
		return stripos($_SERVER['SERVER_NAME'],'expono.com') !== false;
	}
	return false;
}


